/*
 * NewJFrame.java
 *
 * Created on November 26, 2007, 2:20 PM
 */

package guidemointocm;
import java.text.DecimalFormat;

/**
 *
 * @author  Administrator
 */
public class NewJFrame extends javax.swing.JFrame {
    
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        jInchesText = new javax.swing.JTextField();
        jCentimetersText = new javax.swing.JTextField();
        jCalcButton = new javax.swing.JButton();
        jClearButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jINtoCMRadio = new javax.swing.JRadioButton();
        jCMtoINRadio = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Inches to Centimeters");

        jCalcButton.setText("Calculate");
        jCalcButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCalcButtonActionPerformed(evt);
            }
        });

        jClearButton.setText("Clear");
        jClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jClearButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Measurement in Inches:");

        jLabel2.setText("Measurement in Centimeters:");

        buttonGroup1.add(jINtoCMRadio);
        jINtoCMRadio.setSelected(true);
        jINtoCMRadio.setText("IN => CM");
        jINtoCMRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jINtoCMRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(jCMtoINRadio);
        jCMtoINRadio.setText("CM => IN");
        jCMtoINRadio.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCMtoINRadio.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jCentimetersText)
                            .addComponent(jInchesText, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jCalcButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                        .addComponent(jClearButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jINtoCMRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                        .addComponent(jCMtoINRadio)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jInchesText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCentimetersText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jINtoCMRadio)
                    .addComponent(jCMtoINRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jClearButton)
                    .addComponent(jCalcButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jClearButtonActionPerformed
        jInchesText.setText("");
        jCentimetersText.setText("");
    }//GEN-LAST:event_jClearButtonActionPerformed

    private void jCalcButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCalcButtonActionPerformed
        //create our formatting string
        String fmt = "0.###";
        DecimalFormat df = new DecimalFormat(fmt);
        //in inches = null we want to convert cm to in
        if (jCMtoINRadio.isSelected())
        {
            double centimeters = Double.parseDouble(jCentimetersText.getText());
            jInchesText.setText(df.format(centimeters / 2.54).toString());
        }
        //else we want to convert in to cm
        else if (jINtoCMRadio.isSelected())
        {
            double inches = Double.parseDouble(jInchesText.getText());
            jCentimetersText.setText(df.format(inches * 2.54).toString());
        }
    }//GEN-LAST:event_jCalcButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton jCMtoINRadio;
    private javax.swing.JButton jCalcButton;
    private javax.swing.JTextField jCentimetersText;
    private javax.swing.JButton jClearButton;
    private javax.swing.JRadioButton jINtoCMRadio;
    private javax.swing.JTextField jInchesText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
    
}
